package io.renren.modules.wx.controller;

import io.renren.modules.wx.entity.PrizeDTO;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.wx.entity.PrizeEntity;
import io.renren.modules.wx.service.PrizeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-05-07 17:53:05
 */
@RestController
@RequestMapping("wx/prize")
public class PrizeController {

  @Autowired
  private PrizeService prizeService;

  /**
   * 列表
   */
  @RequestMapping("/list")
  @RequiresPermissions("wx:prize:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = prizeService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{id}")
  @RequiresPermissions("wx:prize:info")
  public R info(@PathVariable("id") Integer id) {
    PrizeEntity prize = prizeService.getById(id);

    return R.ok().put("prize", prize);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  @RequiresPermissions("wx:prize:save")
  public R save(String name, Double chance, String desc, Integer sort, MultipartFile image) {

    PrizeEntity entity = new PrizeEntity();
    InputStream in = null;
    try {
      entity.setImage(image.getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    }
    entity.setSort(sort);
    entity.setName(name);
    entity.setChance(chance);
    entity.setSort(sort);

    prizeService.save(entity);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  @RequiresPermissions("wx:prize:update")
  public R update(Integer id, String name, Double chance, String desc, Integer sort,
    MultipartFile image) {

    PrizeEntity entity = new PrizeEntity();
    InputStream in = null;
    try {
      entity.setImage(image.getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    }
    entity.setSort(sort);
    entity.setName(name);
    entity.setChance(chance);
    entity.setSort(sort);
    entity.setId(id);
    prizeService.updateById(entity);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  @RequiresPermissions("wx:prize:delete")
  public R delete(@RequestBody Integer[] ids) {
    prizeService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

}
